﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class Manage_SelectAnnex : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Save_Click(object sender, EventArgs e)
    {
        if (UploadFile.HasFile)
        {
            //保存提交的文件
            DateTime time = DateTime.Now;

            //判断Upload文件夹下面是否包含当前年、月的文件夹
            int year = time.Year;
            int month = time.Month;

            //映射Upload文件夹
            string path = Server.MapPath("~/Upload");

            #region 检查文件夹是否存在，不存在则创建
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            #endregion 


            string folder = string.Format("{0}/{1}", path, Convert.ToString(year));
            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }
            folder = string.Format("{0}/{1}/{2}", path, Convert.ToString(year), Convert.ToString(month));
            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }

            string uuid = Toolkit.NextUUID;
            string dst = UploadFile.FileName;
            string name = dst.Substring(0, dst.LastIndexOf('.'));
            string ext = dst.Substring(dst.LastIndexOf('.') + 1);
            //将文件保存到folder里面
            folder = string.Format("{0}/{1}.{2}", folder, uuid, ext);

            UploadFile.SaveAs(folder);

            Annex a = new Annex();
            a.ID = uuid;
            a.ArticleID = null;
            a.Title = name;
            a.Extension = ext;
            a.Url = "";

            CmsDataContext db = new CmsDataContext();
            db.Annex.InsertOnSubmit(a);
            db.SubmitChanges();

            Response.Write(string.Format("<script>window.returnValue='{0}|{1}|{2}';window.close();</script>", uuid, name, ext));
        }
        else
        {
            Response.Write("<script>window.close();</script>");
        }
    }
}